package com.shaoshaohuo.app.ui;

import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.ExchangeRecord;
import com.shaoshaohuo.app.entity.ExchangeRecordEntity;
import com.shaoshaohuo.app.entity.PointMall;
import com.shaoshaohuo.app.entity.PointMallEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.adapter.ExchangeRecordAdapter;
import com.shaoshaohuo.app.adapter.PointMallItemAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

/**
 * @ClassName PointsMallActivity
 * @Decription 积分商城
 * @2015-8-6 下午9:50:43
 */
public class PointsMallActivity extends BaseActivity implements OnClickListener {

    private TopbarView mTopbarView;

    private GridView mGridview;
    private XListView mListView;

    private TextView mDuihuanText;
    private TextView mJiluText;

    private int size = 200;
    private String action = ListViewAction.NEW;
    private String cursor = "";

    private PointMallItemAdapter<PointMall> mAdapter;
    private ExchangeRecordAdapter<ExchangeRecord> mExchangeRecordAdapter;

    private List<ExchangeRecord> recordList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_point_mall);
        initView();
        setUpView();
        requestData(true);
    }

    private void requestData(boolean b) {
        if (b) {
            startLoadingDialog();
        }
        RequestService.getInstance().getPointMallList(this, size + "", action, cursor, PointMallEntity.class,
                new RequestListener() {

                    @Override
                    public void onSuccess(int requestCode, BaseEntity resultData) {
                        dismissLoadingDialog();
                        if (resultData.isOk()) {
                            PointMallEntity entity = (PointMallEntity) resultData;
                            setAdapter(entity);
                        } else {
                            showToast(resultData.getMsg());
                        }

                    }

                    @Override
                    public void onFailed(int requestCode, Exception error, String msg) {
                        dismissLoadingDialog();
                        showToast(R.string.please_check_network);
                    }
                });
    }

    protected void setAdapter(PointMallEntity entity) {
        if (mAdapter == null) {
            mAdapter = new PointMallItemAdapter<>(this, entity.getData().getList(), false);
            mGridview.setAdapter(mAdapter);
        }
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
        mGridview = (GridView) this.findViewById(R.id.gv_service_list);
        mListView = (XListView) this.findViewById(R.id.listview_data_list);

        mDuihuanText = (TextView) this.findViewById(R.id.textview_duihuan);
        mJiluText = (TextView) this.findViewById(R.id.textview_jilu);
    }

    private void setUpView() {
        mTopbarView.setCenterText("积分商城");
        mTopbarView.setLeftView(true, true);

        mDuihuanText.setOnClickListener(this);
        mJiluText.setOnClickListener(this);

        mDuihuanText.setSelected(true);
        mJiluText.setSelected(false);

        mListView.setPullLoadEnable(false);
        mListView.setPullRefreshEnable(false);
        
        mGridview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PointMall pointMall = (PointMall) parent.getItemAtPosition(position);
                Intent intent = new Intent(PointsMallActivity.this, PointExchangeActivity.class);
                intent.putExtra(ExtraName.orderid, pointMall.getGoodsid());
                startActivity(intent);
            }
        });
        
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ExchangeRecord pointMall = (ExchangeRecord) parent.getItemAtPosition(position);
                Intent intent = new Intent(PointsMallActivity.this, PointExchangeDetailActivity.class);
                intent.putExtra(ExtraName.pointGoods, pointMall);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.textview_duihuan:
            if (mDuihuanText.isSelected()) {
                return;
            }
            mDuihuanText.setSelected(true);
            mJiluText.setSelected(false);

            mGridview.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);

            break;
        case R.id.textview_jilu:
            if (mJiluText.isSelected()) {
                return;
            }
            mDuihuanText.setSelected(false);
            mJiluText.setSelected(true);

            mGridview.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
            if(recordList == null || recordList.isEmpty()){
                getExchangeList();
            }
            break;
        }
    }
    
    private void getExchangeList() {
        startLoadingDialog();
        RequestService.getInstance().exchangeGoodsNotes(this, size+"", action, cursor, ExchangeRecordEntity.class, new RequestListener() {
            
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if(resultData.isOk()){
                    ExchangeRecordEntity entity = (ExchangeRecordEntity) resultData;
                    setExchangeNotesAdapter(entity.getData().getList());
                }else{
                    showToast(resultData.getMsg());
                }
            }
            
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });
    }

    protected void setExchangeNotesAdapter(List<ExchangeRecord> list) {
        this.recordList = list;
        if(mExchangeRecordAdapter == null){
            mExchangeRecordAdapter = new ExchangeRecordAdapter<>(this, list, false);
            mListView.setAdapter(mExchangeRecordAdapter);
        }
    }
	
}
